package com.xuecheng.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.mapper.XcUserRoleMapper;
import com.xuecheng.ucenter.model.dto.RegisterDto;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.model.po.XcUserRole;
import com.xuecheng.ucenter.service.RegisterUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class RegisterServiceImpl implements RegisterUserService {

    @Autowired
    private XcUserMapper xcUserMapper;

    @Autowired
    private XcUserRoleMapper xcUserRoleMapper;

    /**
     * 注册用户
     * @param registerDto
     * @return
     */
    @Override
    @Transactional
    public String Register(RegisterDto registerDto) {
        //判断是否重复
        String cellphone = registerDto.getCellphone();
        String email = registerDto.getEmail();

        //不做格式校验了
        if (cellphone==null || StringUtils.isEmpty(email)){
            throw new RuntimeException("邮箱或手机号为空");
        }

        //验证码校验
        String checkcodekey = "1234";
        String checkcode = registerDto.getCheckcode();
        if (!check(checkcode,checkcodekey)){
            throw new RuntimeException("验证码错误");
        }
        //检查是否已经注册
        if (xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getCellphone, cellphone).eq(XcUser::getEmail, email))!=null){
            throw new RuntimeException("该手机号或邮箱已经绑定了账号");
        }

        //检查账号是否存在
        String username = registerDto.getUsername();
        if (xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getUsername,username))!=null){
            throw new RuntimeException("该用户已经注册过了");

        }

        //校验密码
        String confirmpwd = registerDto.getConfirmpwd();
        String password = registerDto.getPassword();
        if (!check(confirmpwd,password)){
            throw new RuntimeException("两次密码不一致");
        }

        //向用户表中插入数据
        XcUser xcUser = new XcUser();
        BeanUtils.copyProperties(registerDto,xcUser);
        xcUser.setPassword(new BCryptPasswordEncoder().encode(password));
        String userId = UUID.randomUUID().toString();
        xcUser.setId(userId);
        xcUser.setUtype("101001");
        xcUser.setName("学生");
        xcUser.setCreateTime(LocalDateTime.now());
        xcUser.setStatus("1");
        xcUserMapper.insert(xcUser);

        //向用户权限表中插入数据
        XcUserRole xcUserRole = new XcUserRole();
        xcUserRole.setId(UUID.randomUUID().toString());
        xcUserRole.setUserId(userId);
        xcUserRole.setRoleId("17");
        xcUserRole.setCreateTime(LocalDateTime.now());
        xcUserRoleMapper.insert(xcUserRole);

        return "保存成功";
    }

    /**
     * 校验参数
     *
     * @return
     */
    private boolean check(String param1, String param2) {
        if (param1 == null) {
            return false;
        }
        if (param1.equals(param2)) {
            return true;
        }
        return false;
    }
}
